package com.kb.erp.util;

import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther:chenzx
 * @Date:2023-11-23
 * @Description:消息发送
 */
public class MessageUtil {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(MessageUtil.class);

    /**
     * 小助手通知
     *
     * @param user
     * @param content
     */
    public static void sentMessageToAssistant(String user, String content) {
        content = "【定时任务平台】\n" + content;
        try {
            String post = HttpUtil.get("http://192.168.10.22:4087/api/Send/sendText?touser=" + user + "&content=" + content);
            log.info(post);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void sentMessage(String user, String content,String title) {
//        content = "<div class='normal'>"+title+"</div>" + "<div class='highlight'>"+content + "</div>";
        content = "<div class='highlight'>"+content + "</div>";
        try {
            // 构造请求体内容
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("title", title);
            requestBody.put("type", "kb_wechat");  // type写死为wechatwork
            requestBody.put("from", "DTToExternal");  // from为项目唯一code
            requestBody.put("to", user);  // 用户名作为to参数
            requestBody.put("async", true);  // async写死为true
            requestBody.put("msg_type", "text");  // msg_type写死为text
            requestBody.put("description", content);  // content为传入的消息内容
            // 将请求体转换为JSON格式
            String jsonRequestBody = new ObjectMapper().writeValueAsString(requestBody);

            // 日志打印请求体
            log.info("Request body: " + jsonRequestBody);

            // 发送HTTP POST请求
            String url = "http://192.168.10.244:9080/msg";
            String postResponse = HttpUtil.post(url, jsonRequestBody);

            // 打印响应结果
            log.info("Response: " + postResponse);
        } catch (Exception e) {
            // 捕获异常并抛出运行时异常
            log.error("Error occurred while sending message: ", e);
            throw new RuntimeException(e);
        }
    }
}
